package com.worthtech.billportal.dao.hibernate;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Date;
import java.util.List;

import com.worthtech.billportal.bean.CsSysTerminal;
import com.worthtech.billportal.bean.WtMgrEncKey;
import com.worthtech.billportal.bean.WtMgrMerInfo;
import com.worthtech.billportal.bean.WtTermInfo;
import com.worthtech.billportal.dao.SysTerminalDAO;
import com.worthtech.billportal.util.AbstractManager;

public class SysTerminalHibernateDAO extends AbstractManager implements
		SysTerminalDAO {

	public List getListByHQL(String tableName, int startPage, int perNo,
			String orderby) {
		String hql = "from " + tableName + " " + orderby;
		System.out.println("hql2=" + hql);
		if(perNo==0){
			return this.getHibernateTemplate().find(hql);
		}
		return this.getSession().createQuery(hql).setFirstResult(startPage)
				.setMaxResults(perNo).list();
	}

	public void save(CsSysTerminal terminal) {
		// TODO Auto-generated method stub
		this.getHibernateTemplate().save(terminal);
	}
	
	public String getSeqId(String tablename){
		return this.getSession().createSQLQuery("SELECT "+tablename+".NEXTVAL FROM USER_SEQUENCES").list().get(0).toString();
	}

	public boolean update(CsSysTerminal terminal) {
		try {
			this.getHibernateTemplate().update(terminal);
			return true;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		}
	}
	public String getCount(String tableName, String condition){
		String hql = "select count(*) from " + tableName + " " + condition;
		System.out.println("hql1=" + hql);
		String tmp = this.getSession().createQuery(hql).list().get(0)
				.toString();
//		System.out.println("count=" + tmp);
		return tmp;
	}

	public void save(WtTermInfo wti) {
		this.getHibernateTemplate().save(wti);
	}
//
	public void save(WtMgrEncKey w) {
//		this.getHibernateTemplate().save(wtmegkey);
		Connection con;
		PreparedStatement ps;
		String sql = "insert into wt_mgr_enc_key (MGR_INS_ID_CD, MGR_SYS_INDEX, MGR_TERM_ID, MGR_MER_ID, MGR_ENC_KEY_TP, MGR_KEY_SEQ, MGR_ENC_KEY_INDEX, MGR_ENC_KEY_LEN, MGR_ENC_KEY_VAL, " +
				        "MGR_KEY_CHK_VAL, MGR_RESET_ST, MGR_SWITCH_INTERVAL, MGR_LAST_RESET_TIME_TS, MGR_RESET_ENC_KEY_INDEX, MGR_RESET_ENC_KEY_LEN, MGR_RESET_ENC_KEY_VAL, MGR_ALGO_INDEX, MGR_BMK_INDEX, MGR_MN_KEY_INDEX," +
				        " MGR_MN_KEY, MGR_ERR_ALARM_NUM, MGR_TRADE_TYPE, MGR_BATCH_NO, MGR_OPEN_IN, MGR_RUN_IN, MGR_KEY_MN_DES, MGR_OPER_IN, MGR_REC_ID, MGR_REC_UPD_USR_ID, MGR_REC_UPD_TS, MGR_REC_CRT_TS) values " +
				       "('"+w.getId().getMgrInsIdCd()+"',"+w.getId().getMgrSysIndex()+",'"+w.getId().getMgrTermId()+"','"+w.getId().getMgrMerId()+"','"+w.getId().getMgrEncKeyTp()+"','"+w.getId().getMgrKeySeq()+"','"+w.getMgrEncKeyIndex()+"',"+w.getMgrEncKeyLen()+",'"+w.getMgrEncKeyVal()+"'," +
						"'" +w.getMgrKeyChkVal()+"','"+w.getMgrResetSt()+"',"+w.getMgrSwitchInterval()+",sysdate,'"+w.getMgrResetEncKeyIndex()+"',"+w.getMgrResetEncKeyLen()+",'"+w.getMgrResetEncKeyVal()+"','"+w.getMgrAlgoIndex()+"','"+w.getMgrBmkIndex()+"','"+w.getMgrMnKeyIndex()+"'," +
								"'" +w.getMgrMnKey()+"',"+w.getMgrErrAlarmNum()+",'"+w.getMgrTradeType()+"','"+w.getMgrBatchNo()+"','"+w.getMgrOpenIn()+"','"+w.getMgrRunIn()+"','"+w.getMgrKeyMnDes()+"','"+w.getMgrOperIn()+"',"+w.getMgrRecId()+",'"+w.getMgrRecUpdUsrId()+"',sysdate,sysdate)";
		System.out.println(sql);
		con = this.getSession().connection();
		try {
			ps = con.prepareStatement(sql);
			int i= ps.executeUpdate();
//			con.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public void save(WtMgrMerInfo wtmgrmi) {
		this.getHibernateTemplate().save(wtmgrmi);
	}

}
